package com.cmccgz.udacs_landlord_webapp.main.warning.view;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.DevicesStatusAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;
import com.cmccgz.udacs_landlord_webapp.main.warning.WarningActivity;
import com.cmccgz.udacs_landlord_webapp.main.warning.contracts.DevicesStatusContract;
import com.cmccgz.udacs_landlord_webapp.main.warning.presenters.DevicesStatusPresenter;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 带状态的设备列表fragment
 */
public class DevicesStateFragment extends BaseFragment implements View.OnClickListener, OnRvItemClickListenter<DeviceInfoBean>,DevicesStatusContract.View, XRecyclerView.LoadingListener {
    private XRecyclerView rv_devices;
    private LinearLayoutManager layoutManager;
    private List<DeviceInfoBean> datas=new ArrayList<>();
    private DevicesStatusAdapter adapter;
    private ViewHelper viewHelper;
    private DevicesStatusPresenter mPresenter;
    private WarningActivity parentActivity;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_devices_sel;
    }

    @Override
    protected void initView() {
        parentActivity = ((WarningActivity) getActivity());
        viewHelper = ViewHelper.instance(this);
        viewHelper.setAllVisable(false);
        viewHelper.setBackTextVisable(true);
        viewHelper.setTitle("设备列表");

        rv_devices= (XRecyclerView) findViewById(R.id.rv_devices);
        layoutManager=new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        adapter=new DevicesStatusAdapter(datas,mContext,this);
        rv_devices.setLayoutManager(layoutManager);
        rv_devices.setAdapter(adapter);
        rv_devices.setLoadingListener(this);
        rv_devices.setRefreshProgressStyle(ProgressStyle.SysProgress);
        rv_devices.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        rv_devices.addItemDecoration(new MyDividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        rv_devices.setLoadingMoreEnabled(false);

        mPresenter = new DevicesStatusPresenter(this);

        refreshDatas(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                parentActivity.onBackPressed();
                break;
        }
    }

    @Override
    public void onItemClick(final DeviceInfoBean deviceInfoBean, int position, View itemView) {
        if (deviceInfoBean==null) {
            return;
        }
        boolean isWarning = "1".equals(deviceInfoBean.status);
        if (isWarning) {
            ToastUtils.show(mContext,"正在维修中，请勿重复提单！");
            return;
        }
        parentActivity.onBackPressed();
        itemView.postDelayed(new Runnable() {
            @Override
            public void run() {
                parentActivity.setDevice(deviceInfoBean);
            }
        },400);
    }

    @Override
    public void getDevicesSuccess(List<DeviceInfoBean> devices) {
        datas.clear();
        if (devices!=null) {
            datas.addAll(devices);
        }
        adapter.notifyDataSetChanged();
        dismissLoadingDialog();
        rv_devices.refreshComplete();

    }

    @Override
    public void getDevicesError(String msg) {
        showToast(msg);
        dismissLoadingDialog();
        rv_devices.refreshComplete();
    }

    @Override
    public void onRefresh() {
        refreshDatas(false);
    }

    @Override
    public void onLoadMore() {

    }
    private void refreshDatas(boolean isShowDialog) {
        if (isShowDialog) {
            showLoadingDialog("正在加载数据...");
        }
        mPresenter.getDevices();
    }
}
